/*
 * Copyright 2009 Tran Nhut Trung.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.gdevelop.gwtxp.rebind.gwt;

import com.gdevelop.gwtxp.rebind.Tag;
import com.gdevelop.gwtxp.rebind.TagFactory;

public class GWTTagFactory implements TagFactory{
  private String TAG_PACKAGE_NAME = "com.gdevelop.gwtxp.rebind.gwt";
  
  public GWTTagFactory() {
  }

  public Tag createTag(String tagName) {
    Tag tag = null;
    if ("gwtxp".equals(tagName)){
      tag = new GwtXPRootTag();
    }
    
    if ("html".equals(tagName)){
      tag = new HTMLTag();
    }
    if (tag != null){
      return tag;
    }
    
    String className = TAG_PACKAGE_NAME + "."
                + tagName.substring(0, 1).toUpperCase() + tagName.substring(1)
                + "Tag";
    try {
      Class clazz = Class.forName(className);
      Object obj = clazz.newInstance();
      if (obj instanceof Tag){
        tag = (Tag)obj;
        return tag;
      }
    } catch (Throwable e) {
      return null;
    }
    return null;
  }
}
